/**
 * 
 */
$(function(){
	getPage(1);
	$("#checked_all").removeAttr("checked");
})

function getPage(pageNo){
	$("#back_list").css("display","none");
	$("#checked_all").removeAttr("checked"); 
	$.getJSON("get_news",{"pageNum":pageNo},function(data){
		var page=data[0].npage;
		var size=data[0].nsize;
		$("#news_sum").html("总计"+size+"条记录，共"+page+"页，当前第"+pageNo+"页。");
		var pageNav="";
		for(i=0;i<page;i++){
			pageNav+="<a href='javascript:getPage("+(i+1)+")'>"+(i+1)+"</a>&nbsp;&nbsp;";
		}
		$("#psc").html(pageNav);
		if(pageNo>1){
			var first="<a href='javascript:getPage(1)' title='首页'>首页</a>&nbsp;&nbsp;";
			var previous="<a href='javascript:getPage("+(pageNo-1)+")' title='上一页'>&laquo;上一页</a>&nbsp;&nbsp;";
			$("#psc").html(first+previous+$("#psc").html());
		}
		if(pageNo<data[0].npage){
			var last="<a href='javascript:getPage("+data[0].npage+")' title='尾页'>尾页</a>&nbsp;&nbsp;";
      		var next="<a href='javascript:getPage("+(pageNo+1)+")' title='下一页'>下一页  &raquo;</a>&nbsp;&nbsp;";
      		$("#psc").html($("#psc").html()+next+last);
		}
		$("#news_body").empty();
		$.each(data,function(i,item){
			var str="";
			var href="find_news?id="+item.nid;
			str+="<tr><td><input type='checkbox' name='news_checked' onchange='listenCheckedAllStatus()' value="+item.nid+"></td>" +
					"<td><a href="+href+">"+item.ntitle+"</a></td>" +
					"<td>"+new Date(item.ndate).Format("yyyy-MM-dd hh:mm:ss")+"</td>" +
					"<td>"+item.ntype+"</td>";
			$("#news_body").append(str);
		});
	});
}

function onCheckedChanged(){
	if($("#checked_all").is(':checked')){
		 $("[name='news_checked']").attr("checked",'true');
	}else{
		$("[name='news_checked']").removeAttr("checked"); 
	}
}

function listenCheckedAllStatus(){
	var flag=true;
	var obj=$("[name='news_checked']");
	for(i=0;i<obj.length;i++){
		if(obj[i].checked==false){
			$("#checked_all").removeAttr("checked");
			flag=false;
			break;
		}
	}
	if(flag){
		$("#checked_all").attr("checked",'true');
	}
}

function changeQuryType(){
	var flag=$("#nqtype").val();
	switch(flag){
		case "0":
			$("#title_query").css("display","inline");
			$("#type_query").css("display","none");
			$("#time_query").css("display","none");
			break;
		case "1":
			$("#title_query").css("display","none");
			$("#type_query").css("display","inline");
			$("#time_query").css("display","none");
			break;
		case "2":
			$("#title_query").css("display","none");
			$("#type_query").css("display","none");
			$("#time_query").css("display","inline");
			break;
	}
}

function delNews(){
	var ids="";
	var obj=$("[name='news_checked']");
	for(i=0;i<obj.length;i++){
		if(obj[i].checked){
			ids+=obj[i].value+",";
		}
	}
	ids=ids.substr(0,ids.length-1);
	if(ids==""){
		alert("请选择要删除的项！");
	}else{
		$.getJSON("del_news",{"id":ids},function(data){
			alert(data.info);
			getPage(1);
		});
	}
}

function queryNews(pageNo){
	$("#back_list").css("display","");
	var flag=$("#nqtype").val();
	var queryStr="";
	switch(flag){
		case "0":
			queryStr=$("#nqname").val();
			break;
		case "1":
			queryStr=$("#query_list").val();
			break;
		case "2":
			queryStr=$("#date_start").val()+","+$("#date_stop").val();
			break;
	}
	if(!checkQuery(flag)){
		
	}else{
		$.getJSON("query_news",{"flag":flag,"queryStr":queryStr,"pageNum":pageNo},function(data){
			if(data==""){
				alert("未找到任何数据！");
			}else{
				var page=data[0].npage;
				var size=data[0].nsize;
				$("#news_sum").html("总计"+size+"条记录，共"+page+"页，当前第"+pageNo+"页。");
				var pageNav="";
				for(i=0;i<page;i++){
					pageNav+="<a href='javascript:queryNews("+(i+1)+")'>"+(i+1)+"</a>&nbsp;&nbsp;";
				}
				$("#psc").html(pageNav);
				if(pageNo>1){
					var first="<a href='javascript:queryNews(1)' title='首页'>首页</a>&nbsp;&nbsp;";
					var previous="<a href='javascript:queryNews("+(pageNo-1)+")' title='上一页'>&laquo;上一页</a>&nbsp;&nbsp;";
					$("#psc").html(first+previous+$("#psc").html());
				}
				if(pageNo<data[0].npage){
					var last="<a href='javascript:queryNews("+data[0].npage+")' title='尾页'>尾页</a>&nbsp;&nbsp;";
		      		var next="<a href='javascript:queryNews("+(pageNo+1)+")' title='下一页'>下一页  &raquo;</a>&nbsp;&nbsp;";
		      		$("#psc").html($("#psc").html()+next+last);
				}
				$("#news_body").empty();
				$.each(data,function(i,item){
					var str="";
					var href="find_news?id="+item.nid;
					str+="<tr><td><input type='checkbox' name='news_checked' onchange='listenCheckedAllStatus()' value="+item.nid+"></td>" +
							"<td><a href='"+href+"'>"+item.ntitle+"</a></td>" +
							"<td>"+new Date(item.ndate).Format("yyyy-MM-dd hh:mm:ss")+"</td>" +
							"<td>"+item.ntype+"</td>";
					$("#news_body").append(str);
				});
			}
		});
	}	
}

function checkQuery(flag){
	var queryStr="";
	var result=true;
	if(flag=="0"){
		queryStr=$("#nqname").val();
		if(queryStr==""){
			result=false;
			alert("请输入标题关键字！");
		}
	}else if(flag=="2"){
		var start=$("#date_start").val();
		var stop=$("#date_stop").val();
		if(!checkDate(start,stop)){
			result=false;
			alert("查询结束时间需大于开始时间！");
		}
	}
	return result;
}